﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace LED_Tester
{
    public partial class frmLocation : Form
    {
        public frmLocation()
        {
            InitializeComponent();
        }

        #region 控制卡变量

        private bool ctrlIsJoin;
        private const int XCH = 0;
        private const int YCH = 1;
        private const int ZCH = 2;
        private int strVelXCH;
        private int maxVelXCH;
        private int strVelYCH;
        private int maxVelYCH;
        private int strVelZCH;
        private int maxVelZCH;
        private double tacc;
        private int offsetXCH;
        private int offsetYCH;
        private int offsetZCH;
        private double impulseXCH;
        private double impulseYCH;
        private double impulseZCH;

        private int[] old = new int[6];

        private ClassAppConfigure classAppconfigure;

        public bool CtrlIsJoin
        {
            get { return ctrlIsJoin; }
            set { ctrlIsJoin = value; }
        }
        public int StrVelXCH
        {
            get { return strVelXCH; }
            set { strVelXCH = value; }
        }
        public int MaxVelXCH
        {
            get { return maxVelXCH; }
            set { maxVelXCH = value; }
        }
        public int StrVelYCH
        {
            get { return strVelYCH; }
            set { strVelYCH = value; }
        }
        public int MaxVelYCH
        {
            get { return maxVelYCH; }
            set { maxVelYCH = value; }
        }
        public int StrVelZCH
        {
            get { return strVelZCH; }
            set { strVelZCH = value; }
        }
        public int MaxVelZCH
        {
            get { return maxVelZCH; }
            set { maxVelZCH = value; }
        }
        public double Tacc
        {
            get { return tacc; }
            set { tacc = value; }
        }
        public int OffsetXCH
        {
            get { return offsetXCH; }
            set { offsetXCH = value; }
        }
        public int OffsetYCH
        {
            get { return offsetYCH; }
            set { offsetYCH = value; }
        }
        public int OffsetZCH
        {
            get { return offsetZCH; }
            set { offsetZCH = value; }
        }
        public double ImpulseXCH
        {
            get { return impulseXCH; }
            set { impulseXCH = value; }
        }
        public double ImpulseYCH
        {
            get { return impulseYCH; }
            set { impulseYCH = value; }
        }
        public double ImpulseZCH
        {
            get { return impulseZCH; }
            set { impulseZCH = value; }
        }

        #endregion

        #region 功能函数

        private void saveConfig()
        {
            classAppconfigure = new ClassAppConfigure(Application.StartupPath + "\\data\\ini.xml");
            classAppconfigure.MaxVelXCH = maxVelXCH;
            classAppconfigure.MaxVelYCH = maxVelYCH;
            classAppconfigure.MaxVelZCH = maxVelZCH;
            classAppconfigure.OffsetXCH = offsetXCH;
            classAppconfigure.OffsetYCH = offsetYCH;
            classAppconfigure.OffsetZCH = offsetZCH;
            classAppconfigure.Save();
        }

        private static void TxtDataVerifyWithOnePoint2(KeyPressEventArgs e, TextBox txt)
        {
            int pointCount = 0;
            bool isPointExist = false;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)'-')
            {
                e.Handled = false;
                if (txt.Text.Length == 0)
                {
                    if (e.KeyChar == '.')
                        e.Handled = true;
                }

                for (int i = 0; i < txt.Text.Length; i++)
                {
                    if (txt.Text.Substring(i, 1) == ".")
                        pointCount++;
                    if (pointCount == 1)
                        isPointExist = true;
                }

                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }

            else { e.Handled = true; }
        }

        private static void TxtDataVerifyWithoutPoint2(KeyPressEventArgs e, TextBox txt)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)'-')
                e.Handled = false;
            else
                e.Handled = true;
        }

        #endregion

        #region 窗体事件

        private void frmLocation_Load(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                txtYCHNow.Text = ((double)(ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH)).ToString("0.0");
                txtXCHNow.Text = ((double)((ClassCtrlBorad.d1000_get_command_pos(XCH) + (int)impulseXCH * 140) / impulseXCH) * -1).ToString("0.0");
                txtYCHNew.Text = txtYCHNow.Text;
                txtXCHNew.Text = txtXCHNow.Text;

                txtYCHOffset.Text = offsetYCH.ToString();
                txtXCHOffset.Text = offsetXCH.ToString();

                txtYCHSpeed.Text = maxVelYCH.ToString();
                txtXCHSpeed.Text = maxVelXCH.ToString();

                old[0] = offsetYCH;
                old[1] = maxVelYCH;
                old[2] = offsetXCH;
                old[3] = maxVelXCH;
                old[4] = offsetZCH;
                old[5] = maxVelZCH;
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void frmLocation_FormClosing(object sender, FormClosingEventArgs e)
        {
            saveConfig();
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region 按钮事件

        private void btnMove_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                if (txtYCHNew.Text == "" || txtXCHNew.Text == "")
                {
                    MessageBox.Show("请输入一值！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    double thetaTemp = double.Parse(txtYCHNew.Text);
                    double gammaTemp = double.Parse(txtXCHNew.Text);

                    if (thetaTemp < 0 || thetaTemp > 180)
                    {
                        MessageBox.Show("请确认C平面角度在0 - 180°范围！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    if (gammaTemp < -140 || gammaTemp > 140)
                    {
                        MessageBox.Show("请确认γ平面角度在-140 - 140°范围！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }

                int pos = 0;

                pos = (int)(double.Parse(txtYCHNew.Text) * (int)impulseYCH);
                ClassCtrlBorad.d1000_start_ta_move(YCH, pos, strVelYCH, maxVelYCH, Tacc);
                pos = (int)((double.Parse(txtXCHNew.Text) + 140) * (int)impulseXCH) * (-1);
                ClassCtrlBorad.d1000_start_ta_move(XCH, pos, strVelXCH, maxVelXCH, Tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }

                txtYCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH).ToString("#0.0");
                txtXCHNow.Text = ((double)((ClassCtrlBorad.d1000_get_command_pos(XCH) + (int)impulseXCH * 140) / impulseXCH) * -1).ToString("#0.0");

                MessageBox.Show("移动完成！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("请连接控制卡！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnRestroe_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                /** 设置控制卡脉冲输出模式
                  * param : 轴号
                  * param : 0 pulse/dir模式，脉冲上升沿有效
                  *         1 pulse/dir模式，脉冲下降沿有效
                  *         2 CW/CCW模式，脉冲上升沿有效
                  *         3 CW/CCW模式，脉冲下降沿有效
                  */
                ClassCtrlBorad.d1000_set_pls_outmode(YCH, 0);
                ClassCtrlBorad.d1000_set_pls_outmode(XCH, 1);

                ClassCtrlBorad.d1000_out_bit(1, 0);
                // 公转回到原点
                ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
                // 等待公转回到原点
                while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
                // 公转正向偏移
                ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(YCH, 0);

                // 自转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                }
                // 等待自转回到限制位
                while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                // 自转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 自转正确位置
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 自转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }

                txtYCHNow.Text = ((double)(ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH)).ToString("0.0");
                txtXCHNow.Text = ((double)((ClassCtrlBorad.d1000_get_command_pos(XCH) + (int)impulseXCH * 140) / impulseXCH) * -1).ToString("0.0");
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSetYCH_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                if (txtYCHOffset.Text == "" || txtYCHSpeed.Text == "")
                {
                    MessageBox.Show("输入数值错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    old[0] = offsetYCH;
                    old[1] = maxVelYCH;
                    offsetYCH = int.Parse(txtYCHOffset.Text);
                    maxVelYCH = int.Parse(txtYCHSpeed.Text);
                    // 公转回到原点
                    ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
                    // 等待公转回到原点
                    while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
                    // 公转正向偏移
                    ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                    // 公转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                    // 公转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(YCH, 0);

                    txtYCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH).ToString("#0.0");

                    MessageBox.Show("设置成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnReYCH_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                offsetYCH = old[0];
                maxVelYCH = old[1];
                txtYCHOffset.Text = offsetYCH.ToString();
                txtYCHSpeed.Text = maxVelYCH.ToString();
                // 公转回到原点
                ClassCtrlBorad.d1000_home_move(YCH, -strVelYCH, -maxVelYCH, tacc);
                // 等待公转回到原点
                while (ClassCtrlBorad.d1000_check_done(YCH) != 4) { }
                // 公转正向偏移
                ClassCtrlBorad.d1000_start_t_move(YCH, 19200, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                // 公转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(YCH, 0);
                // 公转正确位置
                ClassCtrlBorad.d1000_start_ta_move(YCH, offsetYCH, strVelYCH, maxVelYCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(YCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(YCH, 0);

                txtYCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(YCH) / impulseYCH).ToString("0.0");

                MessageBox.Show("恢复成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSetXCH_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                if (txtXCHOffset.Text == "" || txtXCHSpeed.Text == "")
                {
                    MessageBox.Show("输入数值错误！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    old[2] = offsetXCH;
                    old[3] = maxVelXCH;
                    offsetXCH = int.Parse(txtXCHOffset.Text);
                    maxVelXCH = int.Parse(txtXCHSpeed.Text);
                    // 自转回到限制位
                    string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                    string flag = status.Substring(status.Length - 2, 1);
                    if (flag != "1")
                    {
                        ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    }
                    // 等待自转回到限制位
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }
                    // 自转偏移设零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 自转正确位置
                    ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                    // 设置计数为零
                    ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                    // 自转移动到负90度
                    ClassCtrlBorad.d1000_start_ta_move(XCH, (int)(-1 * 392 * 50), strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }

                    txtXCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(XCH) / impulseXCH).ToString("#0.0");

                    MessageBox.Show("设置成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnReXCH_Click(object sender, EventArgs e)
        {
            if (ctrlIsJoin)
            {
                offsetXCH = old[2];
                maxVelXCH = old[3];
                txtXCHOffset.Text = offsetXCH.ToString();
                txtXCHSpeed.Text = maxVelXCH.ToString();
                // 自转回到限制位
                string status = Convert.ToString(ClassCtrlBorad.d1000_get_axis_status(XCH), 2);
                string flag = status.Substring(status.Length - 2, 1);
                if (flag != "1")
                {
                    ClassCtrlBorad.d1000_start_tv_move(XCH, strVelXCH, maxVelXCH, tacc);
                    while (ClassCtrlBorad.d1000_check_done(XCH) != 3) { }// 等待自转回到限制位
                }
                // 自转偏移设零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                ClassCtrlBorad.d1000_start_ta_move(XCH, -1 * offsetXCH, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }
                // 设置计数为零
                ClassCtrlBorad.d1000_set_command_pos(XCH, 0);
                // 自转移动到负90度
                ClassCtrlBorad.d1000_start_ta_move(XCH, -10000, strVelXCH, maxVelXCH, tacc);
                while (ClassCtrlBorad.d1000_check_done(XCH) == 0) { }

                txtXCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(XCH) / impulseXCH).ToString();

                MessageBox.Show("设置成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtXCHNow.Text = (ClassCtrlBorad.d1000_get_command_pos(XCH) / impulseXCH).ToString();

                MessageBox.Show("恢复成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("控制卡未连接！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region 其它事件

        private void txtYCHNew_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint2(e, txtYCHNew);
        }

        private void txtXCHNew_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint2(e, txtXCHNew);
        }


        private void txtYCHOffset_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithoutPoint2(e, txtYCHOffset);
        }

        private void txtYCHSpeed_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithoutPoint2(e, txtYCHSpeed);
        }

        private void txtXCHOffset_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithoutPoint2(e, txtXCHOffset);
        }

        private void txtXCHSpeed_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithoutPoint2(e, txtXCHSpeed);
        }

        #endregion
    }
}
